/* RongAiClient.java
 *
 * Project/Product: GWBS2009
 *
 * Modify Information:
 * =============================================================================
 *   Author Date       Description
 *   ---------- ---------- ---------------------------------------------------
 *   严首骅    2009-5-31   First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2009
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package client;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import com.apep.connect.APEPMessageEncoder;
import com.apep.connect.APEPRequest;
import com.apep.connect.APEPResponse;
import com.apep.connect.APEPServiceException;
import com.apep.connect.client.APEPClient;
import com.apep.lib.xml.SampleDOMParser;
import com.apep.lib.xml.SampleElement;


public class RongAiClient extends APEPClient {
    
    private static RongAiClient instance = null;
    public static synchronized RongAiClient getInstance() {

        if (instance == null) {
            instance = new RongAiClient();
            return instance;
        }
        return instance;
    }

    private String configFile = "apep.RemotetInfo.xml";

    public static String Remote = "HUNAN-bashi";
    private HashMap tRemoteInfo = new HashMap();

    public RongAiClient() {
        try {
            this.initialRemotetInfoConfigFromXml();
        } catch (Exception e) {
        }
    }
    
    
    private void initialRemotetInfoConfigFromXml() throws Exception {
        FileInputStream tFileInputStream = null;
        try {
            // 1、取得配置文件中的远端信息
            String tClientInfoXmlPath = sSystemConfigurator.getParam(configFile);

            tFileInputStream = new FileInputStream(tClientInfoXmlPath);

            byte bBuf[] = new byte[102400];
            tFileInputStream.read(bBuf);

            // 2、解析远程连接信息配置文件
            SampleDOMParser tPar = new SampleDOMParser();
            SampleElement tRootElement = tPar.parse(bBuf, "UTF-8");
            ArrayList tList = tRootElement.getElement("RemoteInfoConfig.Client");
            for (int i = 0; i < tList.size(); i++) {
                SampleElement tTmpElement = (SampleElement) tList.get(i);
                String tRemoteId = tTmpElement.getChildElement("RemoteId").getTagValue();
                String tPassword = tTmpElement.getChildElement("Password").getTagValue();
                String tServicePassword = tTmpElement.getChildElement("ServicePassword").getTagValue();
                String tDesKey = tTmpElement.getChildElement("DesKey").getTagValue();
                String tServiceUrl = tTmpElement.getChildElement("ServiceUrl").getTagValue();
                String tVersion = tTmpElement.getChildElement("Version").getTagValue();
                String tCharset = tTmpElement.getChildElement("Charset").getTagValue();
                APEPMessageEncoder tAPEPMessageEncoder = APEPMessageEncoder.getInstance(Integer
                        .parseInt(sSystemConfigurator.getParam("apep.connect.max.msg.len")), tDesKey);
                tRemoteInfo.put(tRemoteId + "|PWD", tPassword);
                tRemoteInfo.put(tRemoteId + "|SERPWD", tServicePassword);
                tRemoteInfo.put(tRemoteId + "|DESKEY", tDesKey);
                tRemoteInfo.put(tRemoteId + "|URL", tServiceUrl);
                tRemoteInfo.put(tRemoteId + "|VER", tVersion);
                tRemoteInfo.put(tRemoteId + "|CHARSET", tCharset);
                tRemoteInfo.put(tRemoteId + "|ENCODER", tAPEPMessageEncoder);

            }
        } catch (Exception e) {
            throw e;
        } finally {
            if (tFileInputStream != null)
                tFileInputStream.close();
        }
    }

    /***************************************************************************
     * 发送订单到APEP
     **************************************************************************/
    public APEPResponse sendToApep(APEPRequest tAPEPRequest) throws APEPServiceException {
        tAPEPRequest.setRequestId(tAPEPRequest.getAction() + System.currentTimeMillis());
        Object tObject = tRemoteInfo.get(Remote + "|VER");
        tAPEPRequest.setVersion(tObject.toString());
        tAPEPRequest.setReceiverId(Remote);
        tAPEPRequest.setPassword(tRemoteInfo.get(Remote + "|PWD").toString());
        tAPEPRequest.setSenderId("GWBS-HuNan");
        //CustomerId必须和.net ServerId一致
        tAPEPRequest.setCustomerId("HUNAN-bashi");
        //System.out.println(tAPEPRequest);
        System.out.println(tAPEPRequest.toXml());
        APEPResponse res = submit(tAPEPRequest);
        String tXml = tAPEPRequest.toXml();
        System.out.println(tXml);
        return res;
    }

    /**
     * @param aConfigFile
     *            the configFile to set
     */
    public void setConfigFile(String aConfigFile) {
        this.configFile = aConfigFile;
    }
}
